package com.culturalCenter.dataCenter.controller.web;

import com.alibaba.fastjson.JSON;
import com.culturalCenter.dataCenter.Utils.ApiResult;
import com.culturalCenter.dataCenter.Utils.ResultCode;
import com.culturalCenter.dataCenter.client.SdkClient;
import com.culturalCenter.dataCenter.globalconfig.Interface.ApiVersion;
import com.culturalCenter.dataCenter.globalconfig.Interface.CustomVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * 停车位
 *
 * @Author zhao
 * @Date 2021-4-27 14:49
 */
@RestController
@RequestMapping("/{version}/parking")
@Api(tags = "停车位")
@ApiVersion(CustomVersion.VERSION_1)
@Slf4j
public class ParkingController {

    @Value("${parking.hostname}")
    private String hostname;

    /**
     * 根据帐号名获取accessToken
     */
    @GetMapping("/accessToken")
    @ApiOperation(value = "根据帐号获取访问token", notes = "根据帐号获取访问token，帐号名如：system")
    @ApiImplicitParam(name = "userName", value = "用户名", paramType = "query", dataType = "String", required = true)
    public String getAccessToken(@RequestParam String userName) {
        String url = hostname + "/ipms/subSystem/generate/token?userName=" + userName;
        Map<String, String> headMap = new HashMap<>();
        headMap.put("Content-Type", "application/json;charset=UTF-8");
        try {
            return SdkClient.get(url, headMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return JSON.toJSONString(ApiResult.instance(ResultCode.FALL));
        }
    }

    @GetMapping("/parkingSpace")
    @ApiOperation(value = "获取停车位信息", notes = "获取整个馆的停车位信息")
    @ApiImplicitParam(name = "accessToken", value = "访问token", paramType = "query", dataType = "String", required = true)
    public String getParkingSpace(@RequestParam String accessToken) {
        String url = hostname + "/ipms/parkinglot/stat";
        Map<String, String> headMap = new HashMap<>();
        headMap.put("Content-Type", "application/json;charset=UTF-8");
        headMap.put("accessToken", accessToken);
        try {
            return SdkClient.get(url, headMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return JSON.toJSONString(ApiResult.instance(ResultCode.FALL));
        }
    }

}
